package com.squirrelhuan.quickhttp.http

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class QuickHttp @JvmOverloads constructor(
    private val baseUrl: String,
    private val interceptors: List<Interceptor> = emptyList(),
    private val gson: Gson = GsonBuilder().create(),
    private val timeoutSeconds: Long = 15L,
    private val token: String? = null
) {

    fun <T : Any> buildRetrofit(service:Class<T>): T {
       val retrofit2 : Retrofit = buildRetrofit()
       return retrofit2.create(service)
    }
    fun buildRetrofit(): Retrofit {
        val clientBuilder = OkHttpClient.Builder()
        // 日志拦截器
        val loggingInterceptor = HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.BODY // 打印请求和响应日志
        }
        //loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
        clientBuilder.addInterceptor(loggingInterceptor)
        clientBuilder.addInterceptor(Interceptor { chain ->
            val builder = chain.request().newBuilder()
            builder.addHeader("Accept", "application/json")
            builder.addHeader("Content-Type", "application/json")
            if (token != null) {
                builder.addHeader("Authorization", token)
            }
            //Settings.token?.let { builder.addHeader("Authorization", "Bearer $it") }
            chain.proceed(builder.build())
        })
        // 缓存目录
//        val cacheDir = File(context.cacheDir , "http_cache")
//        val cacheSize = 10 * 1024 * 1024L // 10 MB
//        clientBuilder.cache(Cache(cacheDir, cacheSize)) // 缓存
        interceptors.forEach { clientBuilder.addInterceptor(it) }
        clientBuilder.connectTimeout(timeoutSeconds, TimeUnit.SECONDS)// 连接超时
            .readTimeout(timeoutSeconds, TimeUnit.SECONDS)// 读取超时
            .writeTimeout(timeoutSeconds, TimeUnit.SECONDS) // 写入超时

        return Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(clientBuilder.build())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
    }

    class Builder {
        private var baseUrl: String = ""
        private val interceptor: MutableList<Interceptor> = mutableListOf()
        private var gson: Gson = GsonBuilder().create()
        private var timeoutSeconds: Long = 15L
        fun setBaseUrl(baseUrl: String) = apply {
            this.baseUrl = baseUrl
        }

        fun addInterceptor(interceptor: Interceptor) = apply {
            this.interceptor.add(interceptor)
        }

        fun setGson(gson: Gson) = apply {
            this.gson = gson
        }

        fun setTimeoutSeconds(timeoutSeconds: Long) = apply {
            this.timeoutSeconds = timeoutSeconds
        }

        fun build(): QuickHttp {
            return QuickHttp(baseUrl, interceptor, gson, timeoutSeconds)
        }
    }

//    private companion object {
//
//        @JvmStatic
//        fun create(baseUrl: String): QuickHttp {
//            return QuickHttp(baseUrl)
//        }
//    }
}